package com.lancoo.answer.view.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import com.lancoo.answer.R
import com.lancoo.answer.util.LancooUtils
import com.lancoo.answer.util.ScaleUtil.addClickScale
import com.lancoo.answer.util.SvgaUtils
import com.opensource.svgaplayer.SVGAImageView

class XgDialogLevelUp(
    context: Context?,
    val lastLevel: Int,
    val currLevel: Int
) : Dialog(context!!, R.style.CustomDialog) {
    /**
     * 提交按钮
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.xg_dialog_level_up)
        window!!.setGravity(Gravity.CENTER) //设置弹窗右对齐
        window!!.setWindowAnimations(R.style.MyDialogScale) //设置动画效果

        //按空白处不能取消动画
//        setCanceledOnTouchOutside(false)
//        setCancelable(false)
        //初始化界面数据
        initView()
    }

    fun initView() {
        val svgaIvBg = findViewById<SVGAImageView>(R.id.svgaIvBg)
        val svgaIvLevel = findViewById<SVGAImageView>(R.id.svgaIvLevel)
        val IvCloseDialog = findViewById<ImageView>(R.id.IvCloseDialog)

        val svgaUtilsA = SvgaUtils(context, svgaIvBg)
        svgaUtilsA.initAnimator()
        svgaUtilsA.startAnimator("佛光普照");

        val svgaUtilsB = SvgaUtils(context, svgaIvLevel)
        svgaUtilsB.initAnimator()
        if(lastLevel>currLevel){
            if(LancooUtils.asciiToString(currLevel.toString()) == "A"){
                svgaUtilsB.startAnimator("A+升级");
            }else if(LancooUtils.asciiToString(currLevel.toString()) == "B"){
                svgaUtilsB.startAnimator("B+升级");
            }else if(LancooUtils.asciiToString(currLevel.toString()) == "C"){
                svgaUtilsB.startAnimator("C+升级");
            }else if(LancooUtils.asciiToString(currLevel.toString()) == "D"){
                svgaUtilsB.startAnimator("D+升级");
            }
        }else{
            if(LancooUtils.asciiToString(currLevel.toString()) == "B"){
                svgaUtilsB.startAnimator("B+降级");
            }else if(LancooUtils.asciiToString(currLevel.toString()) == "C"){
                svgaUtilsB.startAnimator("C+降级");
            }else if(LancooUtils.asciiToString(currLevel.toString()) == "D"){
                svgaUtilsB.startAnimator("D+降级");
            }else if(LancooUtils.asciiToString(currLevel.toString()) == "E"){
                svgaUtilsB.startAnimator("E+降级");
            }
        }

        svgaIvBg.setOnClickListener {
            dismiss()
            svgaUtilsA.stopSVGA()
            svgaUtilsB.stopSVGA()
        }
        svgaIvLevel.setOnClickListener {
            dismiss()
            svgaUtilsA.stopSVGA()
            svgaUtilsB.stopSVGA()
        }

        IvCloseDialog.visibility = View.GONE
        IvCloseDialog.addClickScale()
        IvCloseDialog.setOnClickListener {
            dismiss()
            svgaUtilsA.stopSVGA()
            svgaUtilsB.stopSVGA()
        }
    }

}